@extends('admin.frame_layout')
<?php $top_selected = "系统设置";$page_menu = "角色管理" ?>
@section('content')
    <div class="formbody">
        <form id="roles" action="{{route('admin.roles.save',['id'=>(empty($roles)?0:$roles->id)])}}" method="post">
            <input name="_token" value="{{csrf_token()}}" type="hidden"/>
            <ul class="forminfo" style="margin-top: 23px;">
                <li>
                    <label class="lable"><span class="info">*</span>角色名称</label>
                    <input name="name" type="text" class="dfinput" placeholder="角色名称" value="{{$roles->name or ""}}"/>
                    <i></i>
                </li>

                <?php
                $allPermission = \App\Utils\RolesUtil::allPermissions();
                $allParent = \App\Utils\RolesUtil::allParent($allPermission);
                ?>
                @foreach($allParent as $parent)

                    {{-- 是否超级管理员才可以看到此权限 --}}
                    @if($parent['super'] && ss("admin_super") != 1)
                        <?php continue; ?>
                    @endif

                    <li>
                        <div class="formtitle" style="margin-top: 30px;">
                            <span>
                                <input type="checkbox" name="permissions[]" class="changeToggle"
                                       value="{{$parent['permission']}}"
                                        {{isset($roles)?($roles->can($parent['permission'])?"checked":""):""}}
                                />&nbsp;&nbsp;{{$parent['title']}}
                            </span>
                            <i style="float: left;">{{$parent['des']}}</i>
                        </div>
                        
                        <?php
                        $allChild = \App\Utils\RolesUtil::allChild($parent['permission'], $allPermission);
                        ?>

                        @foreach($allChild as $child)
                            {{-- 是否超级管理员才可以看到此权限 --}}
                            @if($child['super'] && ss("admin_super") != 1)
                                <?php continue; ?>
                            @endif

                            <input type="checkbox" name="permissions[]" value="{{$child['permission']}}" class="option"
                                   title="{{$child['des']}}"
                                    {{isset($roles)?($roles->can($child['permission'])?"checked":""):""}}
                            />&nbsp;{{$child['title']}}&nbsp;&nbsp;&nbsp;&nbsp;
                        @endforeach
                    </li>
                @endforeach
            </ul>

            <ul class="forminfo" style="margin-top: 50px;">
                <li><input type="submit" class="btn" value="确认保存"/></li>
            </ul>
        </form>
    </div>
    <script>
        $("#roles").validate({
            rules: {
                name: "required"
            },
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if (data.code == 0) {
                            toastr['success'](data.msg);
                            setInterval(function () {
                                window.location = "{{route('admin.roles')}}";
                            }, 2000);
                        } else {
                            toastr['error'](data.msg);
                        }
                    }
                });
            }
        });

        //全选反选
        $(function () {
            $('.changeToggle').click(function () {
                if ($(this).is(':checked')) {
                    $(this).parents('li').find(".option").prop('checked', true);
                } else {
                    $(this).parents('li').find(".option").prop('checked', false);
                }
            });
        });
    </script>
@endsection